package com.tenement.auto;

import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.core.keygen.KeyGenerators;
import com.zaxxer.hikari.HikariDataSource;

public class AutoCode {

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql:///docker");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        GlobalConfig globalConfig = createGlobalConfigUseStyle2();
        Generator generator = new Generator(dataSource, globalConfig);
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle2() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        globalConfig.getJavadocConfig()
                .setAuthor("BertXie")
                .setSince("1.0.1");

        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("com.tenement");

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix("sys_")
                .setGenerateTable("sys_order");

        //设置生成 entity 并启用 Lombok
        globalConfig
                .enableEntity()
                .setOverwriteEnable(true)
                .setWithLombok(true);


        //设置生成 mapper
        globalConfig.enableMapper();
        globalConfig.enableController();
        globalConfig.enableService();
        globalConfig.enableMapperXml();
        globalConfig.enableServiceImpl();

        ColumnConfig updateTime = new ColumnConfig()
                .setColumnName("update_time")
                .setOnUpdateValue("now()");

        globalConfig.setColumnConfig(updateTime);

        ColumnConfig createTime = new ColumnConfig()
                .setColumnName("create_time")
                .setOnInsertValue("now()");
        globalConfig.setColumnConfig(createTime);

        //设置id为雪花id
        ColumnConfig id = new ColumnConfig()
                .setColumnName("id")
                .setKeyType(KeyType.Generator)
                .setKeyValue(KeyGenerators.snowFlakeId);
        globalConfig.setColumnConfig(id);

        return globalConfig;
    }
}